from xml.etree.ElementTree import ElementTree, dump, fromstring, Element, tostring


class TransAvidRecipe(object):
    def __init__(self, XMLString = None):
	self.ProjectName = None
	self.Filename    = None
	self.StartTC     = None
	self.GUID        = None
	self.SmbPath     = None
	self.Transcoder  = None
	self.XmlVer	 = None
	self.Clips       = []
	self.MediaDely   = None
	self.AAFDely     = None

	if XMLString:
	    self.fromXMLString(XMLString)

    def fromXMLString(self, XMLString = None):
	if XMLString:
	    element = fromstring(XMLString)
	    if element is not None:
		recipe = element.find("Recipe")
		
		if recipe is not None:
		    self.Transcoder = recipe.attrib['Transcoder']
		    self.XmlVer     = recipe.attrib['XmlVer']
		    
		    ProjectName = recipe.find("ProjectName")
		    if ProjectName is not None:
			self.ProjectName = ProjectName.text
		    Filename    = recipe.find("Filename")
		    if Filename is not None:
			self.Filename = Filename.text
		    StartTC     = recipe.find("StartTC")
		    if StartTC is not None:
			self.StartTC  = StartTC.text
		    GUID	= recipe.find("GUID")
		    if GUID is not None:
			self.GUID     = GUID.text
		    SmbPath	= recipe.find("SmbPath")
		    if SmbPath is not None:
			self.SmbPath  = SmbPath.text
		    Clips	= recipe.findall("Clip")
		    if Clips is not None:
			for clip in Clips:
			    self.Clips.append(TransAvidClip(clip))
		    MediaDely   = recipe.find("MediaDely")
		    if MediaDely is not None:
			self.MediaDely = TransAvidDely(MediaDely)
		    AAFDely     = recipe.find("AAFDely")
		    if AAFDely is not None:
			self.AAFDely   = TransAvidDely(AAFDely)
			
			
    def toXMLString(self):
	root = Element("xml")
	recipe = Element("Recipe")
	projectname = Element("ProjectName")
	filename    = Element("Filename")
	starttc	    = Element("StartTC")
	guid	    = Element("GUID")
	smbpath	    = Element("SmbPath")
	
	for i in self.Clips:
	    recipe.append(i.toElement())


	recipe.append(self.MediaDely.toElement("MediaDely"))
	recipe.append(self.AAFDely.toElement("AAFDely"))

	
	if projectname is not None:
	    projectname.text = self.ProjectName
	if filename    is not None:
	    filename.text    = self.Filename
	if starttc     is not None:
	    starttc.text     = self.StartTC
	if guid	       is not None:
	    guid.text        = self.GUID
	if smbpath     is not None:
	    smbpath.text     = self.SmbPath
	    
	recipe.append(projectname)
	recipe.append(filename)
	recipe.append(starttc)
	recipe.append(guid)
	recipe.append(smbpath)
	
	recipe.attrib['Transcoder'] = self.Transcoder
	recipe.attrib['XmlVer']     = self.XmlVer
	
	root.append(recipe)
	
	return tostring(root)
	
	

class TransAvidClip(object):
    def __init__(self, Element = None):
	self.ClipName    = None
	self.Input       = None
	self.Output      = None
	
	if Element is not None:
	    self.fromElement(Element)	    
	    
    def fromElement(self, Element = None):
	if Element is not None:
	    ClipName = Element.find("ClipName")
	    if ClipName is not None:
		self.ClipName = ClipName.text
	    Input    = Element.find("Input")
	    if Input is not None:
		self.Input    = Input.text
	    Output   = Element.find("Output")
	    if Output is not None:
		self.Output   = Output.text

    
    def toElement(self): 
	root = Element("Clip")
	clipname = Element("ClipName")
	input	 = Element("Input")
	output   = Element("Output")
	
	if clipname is not None:
	    clipname.text = self.ClipName
	if input is not None:
	    input.text	  = self.Input
	if output is not None:
	    output.text   = self.Output
	
	root.append(clipname)
	root.append(input)
	root.append(output)
	
	return root
	

class TransAvidDely(object):
    def __init__(self, Element = None):
	self.Path	 = None
	self.Host	 = None
	self.User        = None
	self.Passwd      = None
	
	if Element is not None:
	    self.fromElement(Element)

    def fromElement(self, Element = None):
	if Element is not None:
	    Path   = Element.find("Path")
	    if Path is not None:
		self.Path = Path.text
	    Host   = Element.find("Host")
	    if Host is not None:
		self.Host = Host.text
	    User   = Element.find("User")
	    if User is not None:
		self.User = User.text
	    Passwd = Element.find("Passwd")
	    if Passwd is not None:
		self.Passwd = Passwd.text

    def  toElement(self, Tag=None):
	if Tag is not None:
	    root = Element(Tag)
	    host = Element("Host")
	    path = Element("Path")
	    user = Element("User")
	    passwd = Element("Passwd")
	    
	    if host is not None:
		host.text = self.Host
	    if path is not None:
		path.text = self.Path
	    if user is not None:
		user.text = self.User
	    if passwd is not None:
		passwd.text = self.Passwd
	
	    root.append(host)
	    root.append(path)
	    root.append(passwd)
	    root.append(user)
	    
	    return root

if __name__=="__main__":
    xmlfile = open("AvidRecipe.xml")
    xmlbuff = xmlfile.read()
    xmlfile.close()
    AvidRecipe = TransAvidRecipe(xmlbuff)
    if AvidRecipe:
	print "ProjectName: " + AvidRecipe.ProjectName
	print "Filename:    " + AvidRecipe.Filename
	print "StartTC:     " + AvidRecipe.StartTC
	print "GUID:        " + AvidRecipe.GUID
	for i in AvidRecipe.Clips:
	    print "Name: " + i.ClipName + ", IN: " + i.Input + ", OUT: " + i.Output
	print "MediaDely:   " + AvidRecipe.MediaDely.Host
	print "AAFDely:     " + AvidRecipe.AAFDely.Host
    
    ele = AvidRecipe.toXMLString()
    print ele
    AvidRecipe = TransAvidRecipe(ele)
    if AvidRecipe:
	print "ProjectName: " + AvidRecipe.ProjectName
	print "Filename:    " + AvidRecipe.Filename
	print "StartTC:     " + AvidRecipe.StartTC
	print "GUID:        " + AvidRecipe.GUID
	for i in AvidRecipe.Clips:
	    print "Name: " + i.ClipName + ", IN: " + i.Input + ", OUT: " + i.Output
	print "MediaDely:   " + AvidRecipe.MediaDely.Host
	print "AAFDely:     " + AvidRecipe.AAFDely.Host
